package com.kdtech.analyse.news;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.analyse.JSoupUtils;
import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.StringUtils;
import com.kdtech.utils.HtmlCleaner;
/**
 * http://www.huanqiu.com/ 环球网
 * @author allen
 */
public class HuanqiuNewsAnalyse implements AnalyseNews {


	
	public boolean isDetailPage(String url) {
		boolean bRet=false;
		String[] regex={
				"http://.*.huanqiu.com/.*/[0-9]{4}-[0-9]{2}/[0-9]+[_0-9]*.html"
				};
		for (int i=0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return bRet;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		NewsMeta news=new NewsMeta();
		if (urlMeta.getHtml() == null) {
		}
		String htmltxt=urlMeta.getHtml();
		String url=urlMeta.getUrl();
		news.setUrl(url);
		String title=null;
		String content=null;
		Long date=null;
		Document doc=Jsoup.parse(htmltxt);
		doc.select(".disclaimerHq").remove();
		doc.select(".conRight").remove();
		doc.select(".dotLineBot").remove();
		doc.select(".rankList").remove();


		title =doc.select("h1").text();
		doc.select("div.reTopics, div.editorSign").remove();
		content=HtmlCleaner.getContentHtml(url,doc.select("div#text,div#efpTxt"));
		date=DateUtils.matchDate(doc.select("div.summary").text());
		if(date==null){
			date=DateUtils.matchDate(doc.select("div.summaryNew").text());
		}
		if (date==null){
			date=JSoupUtils.matchDate(doc, "来源:");
		}
		String author= JSoupUtils.matchAuthor(doc, "来源:");


//  	Pattern p=Pattern.compile(".*\\(([0-9]+/[0-9]+)?\\)");
//    Matcher m=p.matcher(title);
//    if (m.find()) {
//    	String group=m.group(1);
//    	title=title.replace("("+group+")","");
//    }

    news.setTitle(title);
    news.setAuthor(author);
    news.setTitle(StringUtils.trimSpace(title));
	news.setContent(StringUtils.trimSpace(content));
	news.setDate(date);
	  return news;
	}
	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	}
	public static void main(String[] args) {
		HuanqiuNewsAnalyse a=new HuanqiuNewsAnalyse();
		String url="http://w.huanqiu.com/r/MV8wXzgxMjQwMzZfOTBfMTQ0OTUyOTk5Mw==?tt_group_id=6225543710936088834";
		UrlMeta meta=CrawlHTML.responseToURL(url);
		NewsMeta parserHtml=a.parserHtml(meta);
		System.out.println(parserHtml);
	}


	
	public boolean isNeedUpdate(){
		return false;
	}
}
